package com.example.woh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.woh.entity.Homework;
import com.example.woh.entity.Submit;
import com.example.woh.mapper.HomeworkMapper;
import com.example.woh.mapper.SubmitMapper;
import com.example.woh.service.ISubmitService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sail
 * @since 2023-03-24
 */
@Service
@RequiredArgsConstructor
public class SubmitServiceImpl extends ServiceImpl<SubmitMapper, Submit> implements ISubmitService {

    private final SubmitMapper submitMapper;
    private final HomeworkMapper homeworkMapper;

    @Override
    public List<Homework> getUnfinishedList(Integer userId) {
        QueryWrapper<Submit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).le("score",-2);
        return getHomeworkList(queryWrapper);
    }

    @Override
    public List<Homework> getFinishedList(Integer userId) {
        QueryWrapper<Submit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).ge("score",-1);
        return getHomeworkList(queryWrapper);
    }



    private List<Homework> getHomeworkList(QueryWrapper<Submit> queryWrapper){

        List<Submit> submits = submitMapper.selectList(queryWrapper);

        //通过submit获得Homework
        List<Homework> homeworkList = new ArrayList<>();

        for (Submit submit : submits) {
            QueryWrapper<Homework> homeworkQueryWrapper = new QueryWrapper<>();
            Integer homeworkId = submit.getHomeworkId();
            homeworkQueryWrapper.eq("id",homeworkId);
            Homework homework = homeworkMapper.selectOne(homeworkQueryWrapper);
            homeworkList.add(homework);
        }

        return homeworkList;
    }
}
